using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Game3D
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteBatch desenho2D;
        Vector3 camView, camCenter;
        /// <summary>
        /// Array de vertices com pos e cor
        /// </summary>
        List<VertexPositionColor[]> primitivasUsuario;

        BasicEffect efeitoBasico;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            camView = new Vector3(0, 1, 0); 
            camCenter = new Vector3(0, 1, 5);
            
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            desenho2D = new SpriteBatch(GraphicsDevice);
            efeitoBasico = new BasicEffect(GraphicsDevice);
            primitivasUsuario = new List<VertexPositionColor[]>();


            VertexPositionColor[] primitivasUsuarioTemp = new VertexPositionColor[3];
            //PRIMEIRA FACE

            primitivasUsuarioTemp[0] = new VertexPositionColor();
            primitivasUsuarioTemp[0].Position = new Vector3(0, 1.5f, 0);
            primitivasUsuarioTemp[0].Color = Color.Green;

            primitivasUsuarioTemp[1] = new VertexPositionColor();
            primitivasUsuarioTemp[1].Position = new Vector3(-1, 0, 1);
            primitivasUsuarioTemp[1].Color = Color.Red;

            primitivasUsuarioTemp[2] = new VertexPositionColor();
            primitivasUsuarioTemp[2].Position = new Vector3(1, 0, 1);
            primitivasUsuarioTemp[2].Color = Color.Red;

            primitivasUsuario.Add(primitivasUsuarioTemp);

            //SEGUNDA FACE
            primitivasUsuarioTemp[0] = new VertexPositionColor();
            primitivasUsuarioTemp[0].Position = new Vector3(0, 1.5f, 0);
            primitivasUsuarioTemp[0].Color = Color.Red;

            primitivasUsuarioTemp[1] = new VertexPositionColor();
            primitivasUsuarioTemp[1].Position = new Vector3(1, 0, -1);
            primitivasUsuarioTemp[1].Color = Color.Red;

            primitivasUsuarioTemp[2] = new VertexPositionColor();
            primitivasUsuarioTemp[2].Position = new Vector3(-1, 0, -1);
            primitivasUsuarioTemp[2].Color = Color.Red;

            primitivasUsuario.Add(primitivasUsuarioTemp);

            //TERCEIRA FACE
            primitivasUsuarioTemp[0] = new VertexPositionColor();
            primitivasUsuarioTemp[0].Position = new Vector3(0, 1.5f, 0);
            primitivasUsuarioTemp[0].Color = Color.Red;

            primitivasUsuarioTemp[1] = new VertexPositionColor();
            primitivasUsuarioTemp[1].Position = new Vector3(1, 0, -1);
            primitivasUsuarioTemp[1].Color = Color.Red;

            primitivasUsuarioTemp[2] = new VertexPositionColor();
            primitivasUsuarioTemp[2].Position = new Vector3(1, 0, 1);
            primitivasUsuarioTemp[2].Color = Color.Red;

            primitivasUsuario.Add(primitivasUsuarioTemp);

            //QUARTA FACE
            primitivasUsuarioTemp[0] = new VertexPositionColor();
            primitivasUsuarioTemp[0].Position = new Vector3(0, 1.5f, 0);
            primitivasUsuarioTemp[0].Color = Color.Red;

            primitivasUsuarioTemp[1] = new VertexPositionColor();
            primitivasUsuarioTemp[1].Position = new Vector3(-1, 0, 1);
            primitivasUsuarioTemp[1].Color = Color.Red;

            primitivasUsuarioTemp[2] = new VertexPositionColor();
            primitivasUsuarioTemp[2].Position = new Vector3(-1, 0, -1);
            primitivasUsuarioTemp[2].Color = Color.Red;

            primitivasUsuario.Add(primitivasUsuarioTemp);

            //QUINTA FACE
             primitivasUsuarioTemp[0] = new VertexPositionColor();
             primitivasUsuarioTemp[0].Position = new Vector3(1, 0, 1);
             primitivasUsuarioTemp[0].Color = Color.Red;

             primitivasUsuarioTemp[1] = new VertexPositionColor();
             primitivasUsuarioTemp[1].Position = new Vector3(1, 0, -1);
             primitivasUsuarioTemp[1].Color = Color.Red;

             primitivasUsuarioTemp[2] = new VertexPositionColor();
             primitivasUsuarioTemp[2].Position = new Vector3(-1, 0, -1);
             primitivasUsuarioTemp[2].Color = Color.Red;


            primitivasUsuario.Add(primitivasUsuarioTemp);

            efeitoBasico.World = Matrix.Identity;

            efeitoBasico.View = Matrix.CreateLookAt(
                camCenter, //camera posicao
                camView, //camera alvo
                new Vector3(0, 1, 0)); //camera up //tb serve Vector3.Up


            efeitoBasico.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, // campo de visao
                GraphicsDevice.Viewport.AspectRatio, //aspecto da tela 3x4, 16/9
                0.1f,//ponto mais proximo -near
                100.0f);//plano mais distante -far

            //ativando as cores
            efeitoBasico.VertexColorEnabled = true;

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {         
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            efeitoBasico.CurrentTechnique.Passes[0].Apply();

                foreach( VertexPositionColor[] p in primitivasUsuario)
            {
                GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip,//tipo da primitiva, linha/triangulo
                   p, //conjunto de vertices
                   0,//offset
                   1);//contador de primitivas
            }
               


            base.Draw(gameTime);
        }
    }
}
